package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.VarietyTypeMapper;
import com.fantastic.model.pojo.zjs.Advertise;
import com.fantastic.model.pojo.zjs.VarietyType;
import com.fantastic.service.zjs.VarietyTypeService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/5/30 09:18
 * @description: 菜品类型service层接口
 */
@Service
public class VarietyTypeServiceImpl implements VarietyTypeService {

    @Resource
    private VarietyTypeMapper varietyTypeMapper;

    /**
     * 查询
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @param varietyType
     * @return
     */
    @Override
    public List<VarietyType> listVarietyType(VarietyType varietyType) {
        List<VarietyType> varietyTypes = varietyTypeMapper.listVarietyType(varietyType);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), varietyTypes);
        for (VarietyType list : varietyTypes) {
            list.setVarietyTypePicture(FilesUtils.getSlashParse(list.getVarietyTypePicture()));
        }
        return varietyTypes;
    }

    /**
     * 查询单个类型
     *
     * @param varietyTypeId
     * @return
     */
    @Override
    public VarietyType getVarietyType(Integer varietyTypeId) {
        typeIdIsNull(varietyTypeId, RespStatusInfo.SELECT_ERROR);
        VarietyType varietyType = varietyTypeMapper.getVarietyType(varietyTypeId);
        varietyType.setVarietyTypePicture(FilesUtils.getSlashParse(varietyType.getVarietyTypePicture()));
        return varietyType;
    }


    /**
     * 添加菜品类型
     *
     * @param varietyType
     */
    @Override
    public Integer insertVarietyType(MultipartFile picture, VarietyType varietyType) {
        typeNameIsNull(varietyType, RespStatusInfo.INSERT_ERROR);
        //判断文件是否存在
        if (picture != null && picture.getSize() != 0) {
            //判断文件是否支持
            MultipartFileUtils.supportType(picture.getOriginalFilename());
            //进行本地写入
            String picturePath = MultipartFileUtils.writeFile(picture, "菜品");
            varietyType.setVarietyTypePicture(picturePath);
        } else {
            throw new InsertException(RespStatusInfo.FILETYPE_NULL);
        }
        return varietyTypeMapper.insertVarietyType(varietyType);
    }

    /**
     * 修改菜品类型
     *
     * @param varietyType
     */
    @Override
    public Integer updateVarietyType(MultipartFile picture, VarietyType varietyType) {
        typeNameIsNull(varietyType, RespStatusInfo.UPDATE_ERROR);
        if (picture != null && picture.getSize() != 0) {
            //判断文件是否支持
            MultipartFileUtils.supportType(picture.getOriginalFilename());
            //进行本地写入
            String picturePath = MultipartFileUtils.writeFile(picture, "菜品");
            varietyType.setVarietyTypePicture(picturePath);
        }
        return varietyTypeMapper.updateVarietyType(varietyType);
    }


    /**
     * 删除菜品类型信息
     *
     * @param varietyTypeId
     */
    @Override
    public Integer deleteVarietyType(Integer varietyTypeId) {
        typeIdIsNull(varietyTypeId, RespStatusInfo.DELETE_ERROR);
        return deleteVarietyType(varietyTypeId);
    }

    /**
     * 判断菜品类型名是否为空或是空字符串
     *
     * @param varietyType
     * @param respStatusInfo
     */
    public void typeNameIsNull(VarietyType varietyType, RespStatusInfo respStatusInfo) {
        if (varietyType.getVarietyTypeName() != null && varietyType.getVarietyTypeName() != "") {
            return;
        }
        throw new InsertException(respStatusInfo);
    }

    /**
     * 判断菜品类型id是否为空
     *
     * @param varietyTypeId
     */
    public void typeIdIsNull(Integer varietyTypeId, RespStatusInfo respStatusInfo) {
        if (varietyTypeId != null) {
            return;
        }
        throw new InsertException(respStatusInfo);
    }
}
